<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 查询 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="ordersInfo.query"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="orderList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="200">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="address" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{
            scope.row.create_time | getTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="reviseAddress"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-outline"
              size="mini"
              @click="expressAddress(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="ordersInfo.pagenum"
        :page-sizes="[1, 5, 7, 10]"
        :page-size="ordersInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改地址对话框 -->
      <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%">
        <el-form
          :model="addressForm"
          :rules="rulesAddress"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="addressForm.address1"
              :options="cityDate"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 物流信息对话框 -->
      <el-dialog title="提示" :visible.sync="expressAddressVisible" width="50%">
        <el-timeline :reverse="true">
          <el-timeline-item
            v-for="(activity, index) in expressAddressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import axios from '@/axios/user'
import cityDate from './city_data2017_element'
export default {
  name: 'Order',
  data() {
    return {
      ordersInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //订单数据列表
      orderList: [],
      total: 0,
      addressVisible: false, //修改地址对话框
      addressForm: {
        address1: [],
        address2: '',
      },
      rulesAddress: {
        address1: [
          { required: true, message: '请输入省市区', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      cityDate,
      expressAddressInfo: [],
      expressAddressVisible: false,
    }
  },
  methods: {
    async getOrderList() {
      const { data } = await axios.get('/orders', { params: this.ordersInfo })
      if (data.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.$message.success('获取成功')
      //console.log(data)
      this.orderList = data.data.goods
      this.total = data.data.total
    },
    handleSizeChange(val) {
      this.ordersInfo.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.ordersInfo.pagenum = val
      this.getOrderList()
    },
    //修改地址
    reviseAddress() {
      this.addressVisible = true
    },
    handleChange() {},
    //快递信息
    async expressAddress(row) {
      console.log(row)
      const { data } = await axios.get(`/kuaidi/1106975712662`)
      console.log(data)
      if (data.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }
      this.expressAddressInfo = data.data
      this.expressAddressVisible = true
    },
  },
  created() {
    this.getOrderList()
  },
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>